package shopping.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import shopping.dto.UserDto;
import shopping.entity.User;
import shopping.service.interfaces.IUserService;
import shopping.util.ResultCode;
import shopping.util.ResultData;

/**
 * 用户
 * @author JiangHuiJun
 *
 */
@Controller
@RequestMapping("user")
public class UserController {

	private static final Logger LOGGER = LoggerFactory.getLogger(RoleController.class);

	@Resource
	IUserService userServiceImpl;

	@RequestMapping(value = "login.action",consumes="application/json")
	@ResponseBody
	public User logins(@RequestBody User user) {
		
		return user;
	}

	/**
	 * 分页查询
	 * 
	 * @return
	 */
	@RequestMapping("queryPage.action")
	@ResponseBody
	public ResultData queryPage(@RequestBody UserDto user) {
		ResultData resultData = new ResultData();
		try {
			Map<String, Object> content = new HashMap<String, Object>();
			
			if(null != user.getUserName() && !"".equals(user.getUserName()))
				content.put("userName", user.getUserName());
			if(null != user.getName() && !"".equals(user.getName()))
				content.put("name", user.getName());
			if(null != user.getSex() && !"".equals(user.getSex()))
				content.put("sex", user.getSex());
			if(null != user.getEmail() && !"".equals(user.getEmail()))
				content.put("email", user.getEmail());
			if(null != user.getPhone() && !"".equals(user.getPhone()))
				content.put("phone", user.getPhone());
			
			List<User> list = userServiceImpl.findByHqlPage(content, user.getFirstResult()-1, user.getMaxResult());
			int count = userServiceImpl.countByHql(content);
			resultData.setData(list);
			resultData.setCount(count);
		} catch (Exception e) {
			resultData.parse(ResultCode.SYSTEM_ERROR);
			LOGGER.info(e.getMessage());
		}
		return resultData;
	}
	
	@RequestMapping("login")
	public String login() {
		return "login";
	}

	@RequestMapping("index")
	public String index() {
		return "index";
	}
	
	@RequestMapping("welcome")
	public String welcome() {
		return "system/welcome";
	}
	
}
